package com.franklin.ideaplugin.maphelper.actions;

import com.franklin.ideaplugin.api.utils.psi.PsiTreeUtils;
import com.franklin.ideaplugin.api.utils.psi.PsiTypeUtils;
import com.franklin.ideaplugin.maphelper.beans.colmember.IColMember;
import com.franklin.ideaplugin.maphelper.col.declaration.IColDeclaration;
import com.franklin.ideaplugin.maphelper.resolver.impl.map.MapMemberResolverFactory;
import com.franklin.ideaplugin.maphelper.utils.PsiMapUtils;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiType;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Objects;

/**
 * 生成map对应实体类action
 *
 * @author Ye Junhui
 * @since 2024/3/22
 */
public class GenerateMapEntityAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        //目标元素
        PsiElement psiElement = e.getData(CommonDataKeys.PSI_ELEMENT);
        if (Objects.isNull(psiElement)){
            return;
        }
        //寻找map定义
        IColDeclaration mapExpressionDeclaration = PsiMapUtils.findMapExpressionDeclaration(psiElement);
        if (Objects.isNull(mapExpressionDeclaration)){
            return;
        }
        PsiType psiType = mapExpressionDeclaration.getDeclaration().getPsiType();
        if (!PsiTypeUtils.isJavaMapType(psiType)){
            return;
        }

        //解析成员
        Collection<IColMember> colMembers = MapMemberResolverFactory.getInstance()
                .resolveExpression(psiElement,mapExpressionDeclaration);

        System.out.println(colMembers);
    }
}
